package com.supply.dl.widget.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.supply.dl.R;
import com.supply.dl.bean.JobAttachBean;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${qianzhihe} on 2018/5/8.
 * 上传图片管理器
 */

public class GridViewImageAdapter extends android.widget.BaseAdapter {

    private final int HEAD_ABLUME = 1;// 相册
    private final int HEAD_CAREAM = 2;// 相机

    private Context mContext;
    List<JobAttachBean> list;
    boolean isDeleteImage = false;
    int currentItemPosition;

    private OnAddOrDeletClickListener onAddOrDeletClickListener;

    public GridViewImageAdapter(Context cntext, int currentItemPosition, OnAddOrDeletClickListener onAddOrDeletClickListener) {
        this(cntext, currentItemPosition);
        this.onAddOrDeletClickListener = onAddOrDeletClickListener;
    }

    public GridViewImageAdapter(Context cntext, int currentItemPosition) {
        this.mContext = cntext;
        this.currentItemPosition = currentItemPosition;
        list = new ArrayList<>();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        //限制最多上传3张图片
        if (list.size() == 0 || list.size() == 1) {
            return list.size() + 1;
        } else {
            return 3;
        }
    }

    @Override
    public View getView(final int position, View contentView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (contentView == null) {
            holder = new ViewHolder();
            contentView = LayoutInflater.from(mContext).inflate(R.layout.gridview_image_item, null);
            holder.addView = (LinearLayout) contentView.findViewById(R.id.gridview_image_item_addImageViewLayout);
            holder.loadImageLayout = (FrameLayout) contentView.findViewById(R.id.gridview_image_item_flImageView);
            holder.loadImageView = (ImageView) contentView.findViewById(R.id.gridview_image_item_showIv);
            holder.deleteLayout = (LinearLayout) contentView.findViewById(R.id.gridview_image_item_lyDeleteClose);
            contentView.setTag(holder);
        } else {
            holder = (ViewHolder) contentView.getTag();
        }
        if (list.size() > 0) {
            if (position < list.size()) {//加载list中的图片
                holder.addView.setVisibility(View.GONE);
                holder.loadImageLayout.setVisibility(View.VISIBLE);

                String path = StringUtils.isCanUse(list.get(position).attachUrl) ?
                        list.get(position).attachUrl : list.get(position).photoPath;
                Bitmap bitmap = BitmapFactory.decodeFile(path);
                Glide.with(mContext)
                        .load(bitmap)
                        .thumbnail(0.2f)//设置缩略图支持
                        .into(holder.loadImageView);
            } else {
                holder.addView.setVisibility(View.VISIBLE);
                holder.loadImageLayout.setVisibility(View.GONE);
            }

        } else {//如果list里面没有数据，默认显示一个+号图案
            holder.addView.setVisibility(View.VISIBLE);
            holder.loadImageLayout.setVisibility(View.GONE);
        }
        /**
         * 添加上传图片
         */
        final ViewHolder finalHolder = holder;
        holder.addView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isDeleteImage = false;
                if (list.size() == 4) {
                    ToastUtil.showToast(mContext, "最多上传4张图片");
                    return;
                }

                if (onAddOrDeletClickListener != null) {
                    onAddOrDeletClickListener.onAddClick();
                }

//                PictureHelper.toSelectPicture((Activity) mContext);

            }
        });

        /**
         * 删除上传图片
         */
        holder.deleteLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.size() > 0) {
                    isDeleteImage = true;

                    JobAttachBean item = list.get(position);
                    GridViewImageAdapter.this.notifyDataSetChanged();

                    if (onAddOrDeletClickListener != null)
                        onAddOrDeletClickListener.onDeleteClick(item);
                }
            }
        });
        return contentView;
    }

    public void setData(ArrayList<JobAttachBean> mList) {
        this.list = mList;
    }

    class ViewHolder {
        LinearLayout addView;
        FrameLayout loadImageLayout;
        ImageView loadImageView;
        LinearLayout deleteLayout;
    }

    /**
     * 打开相册或者相机
     *
     * @param flag
     */
    private void openAblumeOrCaream(int flag) {
        switch (flag) {
            case HEAD_ABLUME://相册
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                ((Activity) mContext).startActivityForResult(intent, HEAD_ABLUME);
                break;
            case HEAD_CAREAM://相机
                Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                ((Activity) mContext).startActivityForResult(camera, HEAD_CAREAM);
                break;
        }
    }

    /**
     * 之前的 点击不能满足需求,因为在点击的时候需要清除，是点击的哪项，才能正确的显示
     * <p>
     * Author: tian <br>
     * Time: 2018/8/29 16:37
     */
    public interface OnAddOrDeletClickListener {

        void onAddClick();

        void onDeleteClick(JobAttachBean jobAttachBean);

    }
}
